#include "recboxitem.h"
#include "ui_recboxitem.h"

#include <QEvent>
#include <qDebug>
#include <QPropertyAnimation>

recBoxItem::recBoxItem(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::recBoxItem)
{
    ui->setupUi(this);

    ui->musicImageBox->installEventFilter(this); //设置事件拦截器
}

recBoxItem::~recBoxItem()
{
    delete ui;
}

bool recBoxItem::eventFilter(QObject* watched, QEvent* event)
{
    //如果拦截器拦截到的事件是我们想要处理的组件的事件
    if(watched == ui->musicImageBox)
    {
        if(QEvent::Enter == event->type())
        {
            QPropertyAnimation* animation1 = new QPropertyAnimation(ui->musicImageBox, "geometry", this);
            animation1->setDuration(100);
            animation1->setStartValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation1->setEndValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation1->start();
            connect(animation1, &QPropertyAnimation::finished, this, [=](){
                delete animation1;
            });
        }
        else if(QEvent::Leave == event->type())
        {
            QPropertyAnimation* animation2 = new QPropertyAnimation(ui->musicImageBox, "geometry", this);
            animation2->setDuration(100);
            animation2->setStartValue(QRect(9, 0, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation2->setEndValue(QRect(9, 9, ui->musicImageBox->width(), ui->musicImageBox->height()));
            animation2->start();
            connect(animation2, &QPropertyAnimation::finished, this, [=](){
                delete animation2;
            });
        }
    return true;
    }
    return QObject::eventFilter(watched, event);
}

void recBoxItem::setRecBoxItemText(const QString &text)
{
    ui->recBoxItemText->setText(text);
}

void recBoxItem::setMusicImage(const QString &imagepath)
{
    QString style = "background-image: url(" + imagepath + ");";
    ui->recMusicImage->setStyleSheet(style);
}
